﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;

#endregion


namespace ESF.General.Entity
{
	public class CertificateResumen
	{
		#region Members

		public string IssuerName_CN { set; get; }

		public string SubjectName_CN { set; get; }

		public DateTime NotBefore { set; get; }

		public DateTime NotAfter { set; get; }

		public string Thumbprint { set; get; }

		public string SourceType { get { return (FromFile ? "Desde archivo" : "Token (USB) o Instalado"); } }

		public bool FromFile { set; get; }

		[IgnoreDataMember]
		public X509Certificate2 Certificate;

		#endregion


		#region Constructor

		private CertificateResumen() { }


		public CertificateResumen(X509Certificate2 pCertificate, bool pFromFile = false)
		{
			Dictionary<string, string> temp;


			Certificate = pCertificate;

			Thumbprint = pCertificate.Thumbprint;

			temp = TextHelper.Split(pCertificate.IssuerName.Name, ",", '=', '"', false);

			if (temp.ContainsKey("CN"))
			{
				IssuerName_CN = temp["CN"];
			}
			else
			{
				IssuerName_CN = "Desconocido (CN)";
			}


			temp = TextHelper.Split(pCertificate.SubjectName.Name, ",", '=', '"', false);

			if (temp.ContainsKey("CN"))
			{
				SubjectName_CN = temp["CN"];
			}
			else
			{
				SubjectName_CN = "Desconocido (CN)";
			}

			NotAfter = pCertificate.NotAfter;

			NotBefore = pCertificate.NotBefore;

			FromFile = pFromFile;
		}

		#endregion
	}
}
